<?php 
//Include the 2 important files
require_once("functions.php");
require_once("Connections/cnn.php");

//Start the session and take the values
session_start();
$sID = $_SESSION['memberID'];

//Proceed uploading file
$dir="";
$file="";

if (isset($_POST['doUpLoad']))
{
   if ($_FILES["avatar_upload"]["error"] > 0)
   {
      $errorMsg = $_FILES["avatar_upload"]["error"];
	  Redirect('changeavatar.php?errorMsg='."$errorMsg".'');
   }
   else
   {
	   //Check file exists
       if (file_exists("images/" . $_FILES["avatar_upload"]["name"]))
    	{
      	$errorMsg = $_FILES['avatar_upload']['name'] . 'was existed';
		Redirect('changeavatar.php?errorMsg='."$errorMsg".'');
   		}
   		else
   		{  
			if($_FILES["avatar_upload"]["size"] > 30000)
			{
			$errorMsg = "Exceeded filesize limit.";
			Redirect('changeavatar.php?errorMsg='."$errorMsg".'');
			}
			else
			{
				move_uploaded_file($_FILES["avatar_upload"]["tmp_name"],"images/" . $_FILES["avatar_upload"]["name"]);
				
				$URL = addslashes($_REQUEST['hdavatar']);
				//Declare SQL Query
				$sql = "UPDATE Members
				SET Avatar = '{$URL}'
				WHERE ID = {$sID}";
				$updateQuery = @mysql_query("$sql");
			
				//echo $sql;
		
				//Check SQL Query.
				//This is the update query, so the result will return true (1) or false (0)
				if($updateQuery) //if($updateQuery) means if($updateQuery = true) or if($updateQuery = 1)
				{
					//Declare var Msg
					$errorMsg = "Update successfully"; 
					//Redirect is function was define in functions.php
					//Assign the var Msg and var Time into URL and back to editaccount.php
					//errorMsg and fixtime are two var that we will use $_REQUEST['Var Name'] to get the value on next page
					@mysql_close($conn);
					Redirect('changeavatar.php?errorMsg='."$errorMsg".'');	
				}
				else
				{
					//Same of above
					$errorMsg = "Update False"; 
					Redirect('changeavatar.php?errorMsg='."$errorMsg".'');
				}
			} //If check size
   		}//If check exist
    }//If check error
}//If all
?>